﻿package app.view.components.sections.popup 
{
	import app.model.RemoteProxy;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.Section;
	import app.view.events.UIEvent;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	/**
	 * Попап для интервью с подбедителями
	 * @author firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class PopupChildrenWinerInterviewMBase extends Section
	{
		// app.view.components.sections.popup.PopupChildrenWinerInterviewMBase
		
		public var closeBtn:Sprite;
		public var left_Btn:Sprite;
		public var right_Btn:Sprite;
		public var bgd:Sprite;
		public var textVideo:TextField;
		public var textImage:TextField;
		public var bgdImage:Sprite;
		public  var videoBox:VideoBoxChildren;
		
		private var _image:Image;
		private var _currentId:int;
		
		
		public function PopupChildrenWinerInterviewMBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			textVideo.multiline = true;
			textVideo.wordWrap = true;
			textImage.multiline = true;
			textImage.wordWrap = true;
			
			_image = new Image(bgdImage.width,bgdImage.height,Image.RESIZE_MAX, Image.ALIGN_CENTER);
			bgdImage.addChild(_image);
			
		}
		
		private function setEvents():void
		{
			closeBtn.addEventListener(MouseEvent.CLICK, onClosePopup);
			bgd.addEventListener(MouseEvent.CLICK, onClosePopup);
			left_Btn.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
			right_Btn.addEventListener(MouseEvent.CLICK, onClickRightBtn);
			
			closeBtn.buttonMode = closeBtn.useHandCursor = true;
			left_Btn.buttonMode = left_Btn.useHandCursor = true;
			right_Btn.buttonMode = right_Btn.useHandCursor = true;
			
		}
		
		
		private function removeEvents():void
		{
			closeBtn.buttonMode = closeBtn.useHandCursor = false;
			left_Btn.buttonMode = left_Btn.useHandCursor = false;
			right_Btn.buttonMode = right_Btn.useHandCursor = false;
			closeBtn.removeEventListener(MouseEvent.CLICK, onClosePopup);
			bgd.removeEventListener(MouseEvent.CLICK, onClosePopup);
			left_Btn.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
			right_Btn.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			_image.destroy();
			bgdImage.removeChild(_image);
			
			videoBox.destroy();
			videoBox = null;
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			//trace('data popup',_data.data[0].image);
			//trace('data popup',_data.currentId);
			_currentId = _data.currentId;
			
			setContentParam(_currentId);
			
			setEvents();
			
			
			
			// TEST
			//videoBox.source = 'test_movie.flv';
		};
		
		/**
		 * установим нужные параметры сонтенту
		 */
		private function setContentParam(n:int):void
		{
			trace('text_fio',_data.data[n].text_fio);
			
			//setParamHead(HeadMarkerBase.BGD_ORANGE, _data.data[n].text_fio);
			
			if (_data.data[n].image!=RemoteProxy.DOMEN) 
			{
				_image.loadImage(_data.data[n].image);
			}
			if (_data.data[n].flv!=RemoteProxy.DOMEN) 
			{
				videoBox.source = _data.data[n].flv;
			}else
			{
				videoBox.reset();
			}
			
			//var l:Loader = new Loader();
			//var ap:ApplicationDomain = new ApplicationDomain();
			//l.load(r, new LoaderContext(false, ap);
			
			textImage.htmlText = _data.data[n].text_image;
			textVideo.htmlText = _data.data[n].text_video;
			
		}
		
		private function onClosePopup(e:MouseEvent):void 
		{
			trace('onClosePopup');
			removeEvents();
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_CLOSE);
			dispatchEvent(ev);
			
		}
		
		private function onClickLeftBtn(e:MouseEvent):void 
		{
			_currentId = _currentId - 1;
			
			if (_currentId<0)
			{
				_currentId = _data.data.length-1;
			}
			
			setContentParam(_currentId);
			//trace('555',_currentId);
		}
		
		private function onClickRightBtn(e:MouseEvent):void 
		{
			_currentId = _currentId + 1;
			
			if (_currentId>_data.data.length-1) 
			{
				_currentId = 0;
			}
			
			setContentParam(_currentId);
			//trace('444',_currentId);
		}
		
	}

}